package com.xu.t1.common;

import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;


/**
 * ListSplitter 通用列表拆分工具类
 * 提供将一个列表均匀拆分成多个子列表的功能。
 *
 * @author xu
 *
 */

@Component
public class ListSplitter {


    // 通用的List拆分器
    public static <T> List<List<T>> split(List<T> originalList, int partCount) {
        List<List<T>> result = new ArrayList<>();

        if (originalList == null || originalList.isEmpty() || partCount <= 0) {
            return result;
        }

        int totalSize = originalList.size();
        int batchSize = (totalSize + partCount - 1) / partCount;  // 向上取整

        for (int i = 0; i < partCount; i++) {
            int fromIndex = i * batchSize;
            if (fromIndex >= totalSize) break;
            int toIndex = Math.min(fromIndex + batchSize, totalSize);
            result.add(originalList.subList(fromIndex, toIndex));
        }

        return result;
    }
}